package com.itboye.lottery

import android.Manifest
import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat
import android.support.v4.view.ViewPager
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.Toast
import com.itboye.bylibrary.widget.wheelview.view.WheelPicker
import com.itboye.lottery.adapter.MainFragmentAdapter
import com.itboye.lottery.base.BaseActivity
import com.itboye.lottery.fragment.*
import com.itboye.lottery.utils.PermissionListener
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : BaseActivity() {

    private var PERMISSIONS: Array<String> = arrayOf(Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_CONTACTS)
    var ll_home: LinearLayout? = null
    var ll_find: LinearLayout? = null
    var ll_gendan: LinearLayout? = null
    var ll_announcement: LinearLayout? = null
    var ll_mylottery: LinearLayout? = null
    var position = 0
    internal var list: MutableList<Fragment> = java.util.ArrayList()
    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun initData() {
//        throw RuntimeException("错误测试")
        setSelectTab(position)
        initFragment()
//        showImage()
//        requestPermission()
//        showWheekView()
//        test2()
//        checkAndOpennotifycation(this)
    }

    private var mainFragmentAdapter: MainFragmentAdapter? = null

    private fun initFragment() {
        list.add(HomeFragment())
        list.add(FindFragment())
        list.add(GenDanFragment())
        list.add(AnnouncementFragment())
        list.add(MyLotteryFragment())
        mainFragmentAdapter = MainFragmentAdapter(supportFragmentManager, list)
        main_viewPager.adapter = mainFragmentAdapter
        main_viewPager.offscreenPageLimit = 4
        main_viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {

            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })
    }

    private fun showImage() {
//        GlideUtil.display(this, "https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1530239796&di=53b2cfd3ff012d40a5e99a16971cf46b&src=http://imgsrc.baidu.com/imgad/pic/item/6609c93d70cf3bc7553b14f2db00baa1cd112af1.jpg", image)
    }

    private fun requestPermission() {
        requestRunPermisssion(PERMISSIONS, object : PermissionListener {
            override fun onGranted() {
                Toast.makeText(this@MainActivity, "允许了", Toast.LENGTH_SHORT).show()
            }

            override fun onDenied(deniedPermission: MutableList<String>?) {
                if (deniedPermission!!.size > 0) {
                    showPermission(this@MainActivity)
                }
            }

        })
    }

    private fun showWheekView() {
        val view = LayoutInflater.from(this).inflate(R.layout.alertdialog_number, null)
        val myNumberPickerView = view.findViewById(R.id.number_picker1) as WheelPicker
        var arraystring: ArrayList<String>? = ArrayList()
        resources.getStringArray(R.array.timeArray).forEachIndexed { index, s ->
            arraystring!!.add(s)
        }

//        list= Arrays.asList("哈哈","呵呵") as List<String>
        myNumberPickerView.data = arraystring
        myNumberPickerView.selectedItemPosition = 0
        val alert = android.support.v7.app.AlertDialog.Builder(this)
        alert.setPositiveButton("确定") { dialog, which -> }
        alert.setNegativeButton("取消") { dialog, which -> }
        myNumberPickerView.setOnItemSelectedListener { picker, data, position -> }
        alert.setView(view)
        alert.create()
        alert.show()
    }


    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.ll_home -> {
                position = 0
                setSelectTab(position)
            }
            R.id.ll_find -> {
                position = 1
                setSelectTab(position)
            }
            R.id.ll_gendan -> {
                position = 2
                setSelectTab(position)
            }
            R.id.ll_announcement -> {
                position = 3
                setSelectTab(position)
            }
            R.id.ll_mylottery -> {
                position = 4
                setSelectTab(position)
            }
            else -> {
            }
        }
    }

    private fun setSelectTab(position: Int) {
        clearSelectStatus()
        when (position) {
            0 -> {
                ll_home!!.isSelected = true
                tv_home.setTextColor(ContextCompat.getColor(this, R.color.main_color))
            }
            1 -> {
                ll_find!!.isSelected = true
                tv_find.setTextColor(ContextCompat.getColor(this, R.color.main_color))
            }
            2 -> {
                ll_gendan!!.isSelected = true
                tv_gendan.setTextColor(ContextCompat.getColor(this, R.color.main_color))
            }
            3 -> {
                ll_announcement!!.isSelected = true
                tv_announcement.setTextColor(ContextCompat.getColor(this, R.color.main_color))
            }
            4 -> {
                ll_mylottery!!.isSelected = true
                tv_mylottery.setTextColor(ContextCompat.getColor(this, R.color.main_color))
            }
            else -> {
            }
        }
        main_viewPager.currentItem = position
    }

    private fun clearSelectStatus() {
        ll_home!!.isSelected = false
        ll_announcement!!.isSelected = false
        ll_mylottery!!.isSelected = false
        ll_find!!.isSelected = false
        ll_gendan!!.isSelected = false
        tv_home.setTextColor(ContextCompat.getColor(this, R.color.gray_6A))
        tv_announcement.setTextColor(ContextCompat.getColor(this, R.color.gray_6A))
        tv_mylottery.setTextColor(ContextCompat.getColor(this, R.color.gray_6A))
        tv_find.setTextColor(ContextCompat.getColor(this, R.color.gray_6A))
        tv_gendan.setTextColor(ContextCompat.getColor(this, R.color.gray_6A))
    }
}
